package it.eng.area118.mdogpscollector;

import it.eng.area118.mdogpscollector.SearchBarFragment.HomeCallback;
import it.eng.area118.mdogpscollector.command.SearchFilter;
import it.eng.area118.mdogpscollector.util.ViewUtils;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class ViewPagerExample extends FragmentActivity {
	private MyAdapter mAdapter;
	private NonSwipeViewPager mPager;
	private boolean mTwoPane;
	private static SearchBarFragment menu;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_item_list);

		mPager = (NonSwipeViewPager) findViewById(R.id.pager);
		Log.i("TAG", "MPAGER IS: " + mPager);
		if (mPager != null) {
			mAdapter = new MyAdapter(getSupportFragmentManager());
			mTwoPane = false;
			initPagerMode();
		} else {
			mTwoPane = true;
			initNonPagedMode();
		}

	}

	/* for tablet */
	private void initNonPagedMode() {
		if (findViewById(R.id.item_detail_container) != null) {
			mTwoPane = true;
			((ItemListFragment) getSupportFragmentManager().findFragmentById(R.id.item_list)).setActivateOnItemClick(true);
		}
		Button insertBt = ViewUtils.getView(this, R.id.btInsertCheckPoint);
		if (insertBt != null) {
			insertBt.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					if (mTwoPane) {
						InsertCheckPointFragment fragment = new InsertCheckPointFragment();
						FragmentTransaction tr = getSupportFragmentManager().beginTransaction();
						// tr.setCustomAnimations(R.anim.slide_in_left,
						// R.anim.slide_out_right);
						tr.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
						tr.replace(R.id.item_detail_container, fragment).commit();

					} else {
						Intent detailIntent = new Intent(ViewPagerExample.this, InsertCheckPointActivity.class);
						startActivity(detailIntent);
					}
				}
			});
		}
	}

	/* for smartphone */
	private void initPagerMode() {
		mPager.setAdapter(mAdapter);
		menu = new SearchBarFragment();
		// menu.setOnSearchListener(new HomeCallback() {
		//
		// @Override
		// public void onSearch(SearchFilter filter) {
		// mPager.setCurrentItem(1);
		//
		// }
		//
		// @Override
		// public void onInsertNewPoint() {
		// mPager.setCurrentItem(2);
		//
		// }
		// });
		mPager.setCurrentItem(0);
		if (findViewById(R.id.item_detail_container) != null) {
			mTwoPane = true;
			((ItemListFragment) getSupportFragmentManager().findFragmentById(R.id.item_list)).setActivateOnItemClick(true);
		}

		Button insertBt = ViewUtils.getView(this, R.id.btInsertCheckPoint);
		if (insertBt != null) {
			insertBt.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					if (mTwoPane) {
						InsertCheckPointFragment fragment = new InsertCheckPointFragment();
						FragmentTransaction tr = getSupportFragmentManager().beginTransaction();
						// tr.setCustomAnimations(R.anim.slide_in_left,
						// R.anim.slide_out_right);
						tr.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
						tr.replace(R.id.item_detail_container, fragment).commit();

					} else {
						Intent detailIntent = new Intent(ViewPagerExample.this, InsertCheckPointActivity.class);
						// detailIntent.putExtra(ItemDetailFragment.ARG_ITEM_ID,
						// id);
						startActivity(detailIntent);
					}
				}
			});
		}
	}

	public static class MyAdapter extends FragmentPagerAdapter {
		public MyAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public int getCount() {
			return 3;
		}

		@Override
		public Fragment getItem(int position) {
			switch (position) {
			case 0:
				return menu;
			case 1:
				return new ItemListFragment();
			case 2:
				return new InsertCheckPointFragment();

			default:
				return null;
			}
		}
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		if (item.getItemId() == android.R.id.home) {
			mPager.setCurrentItem(0);

		}

		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onBackPressed() {
		mPager.setCurrentItem((mPager.getCurrentItem() - 1) / mPager.getAdapter().getCount());
		// super.onBackPressed();
	}

	// @Override
	// public void onItemSelected(String id) {
	// if (mTwoPane) {
	// Bundle arguments = new Bundle();
	// arguments.putString(ItemDetailFragment.ARG_ITEM_ID, id);
	// ItemDetailFragment fragment = new ItemDetailFragment();
	// fragment.setArguments(arguments);
	//
	// FragmentTransaction tr = getSupportFragmentManager()
	// .beginTransaction();
	// tr.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
	// tr.replace(R.id.item_detail_container, fragment).commit();
	//
	// } else {
	//
	// Intent detailIntent = new Intent(this, ItemDetailActivity.class);
	// detailIntent.putExtra(ItemDetailFragment.ARG_ITEM_ID, id);
	// startActivity(detailIntent);
	// }
	// }

}